package blog.controller;

import blog.entity.Notice;
import blog.service.NoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    @RequestMapping("")
    public String noticeList(Map map){
        List<Notice> noticeList = noticeService.getNoticeList();
        map.put("noticeList",noticeList);
        return "notice/notice-list";
    }
    @RequestMapping("/insert")
    public String gotoInsert(){
        return "notice/notice-add";
    }
    @RequestMapping("/delete/{id}")
    public String deleteNotice(@PathVariable("id") Integer id){
        noticeService.deleteNotice(id);
        return "forward:/notice";
    }
    @RequestMapping("/insertSubmit")
    public String insert(Notice notice){
        notice.setNoticeCreateTime(new Date());
        notice.setNoticeUpdateTime(new Date());
        notice.setNoticeStatus(1);
        noticeService.addNotice(notice);
        return "forward:/notice";
    }
    @RequestMapping("/edit/{id}")
    public String gotoEdit(@PathVariable("id") Integer id,Map map){
        Notice notice = noticeService.getNoticeById(id);
        map.put("notice",notice);
        return "notice/notice-editor";
    }
    @RequestMapping("/updateNotice")
    public String updateNotice(Notice notice){
        notice.setNoticeUpdateTime(new Date());
        noticeService.updateNotice(notice);
        return "forward:/notice";
    }
}
